package com.finalproject.endice9x9;

import android.app.Fragment;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

//9x9 game board fragment
public class GameBoardFragment extends Fragment {
	ImageView[][] gameBoard; 
	TextView textViewLevel;
	GameController controller;
	
	int rows = 0;
	int cols = 0;
	
	int downRow = -1;
	int downCol = -1;
	
	//Possible types for board tile linked to image resource ID
	public enum TILE_TYPES {
		EMPTY(R.drawable.empty_tile),
		DESTINATION(R.drawable.dice_landing),
		DICE0(R.drawable.dice0),
		DICE1(R.drawable.dice1),
		DICE2(R.drawable.dice2),
		DICE3(R.drawable.dice3),
		DICE4(R.drawable.dice4),
		DICE5(R.drawable.dice5),
		DICE6(R.drawable.dice6);
		
		private final int resId;
		
		TILE_TYPES(int resId) {
			this.resId = resId;
		}
		
		public int getResId() {
			return resId;
		}
	}
	
	public GameBoardFragment(GameController controller) {
		this.controller = controller;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_game_board, parent, false);
		
		TableLayout tableLayoutGameBoard = (TableLayout) v.findViewById(R.id.tableLayoutGameBoard);
		//Get number of rows
		rows = tableLayoutGameBoard.getChildCount();
		TableRow tableLayoutRow = (TableRow)tableLayoutGameBoard.getChildAt(0);
		//Get number of columns
		cols = tableLayoutRow.getChildCount();
		//Build game board
		gameBoard = new ImageView[rows][cols];
		//Link board's elements to TableLayout elements
		for(int row = 0; row < rows; row++) {
			tableLayoutRow = (TableRow)tableLayoutGameBoard.getChildAt(row);
			for(int col = 0; col < cols; col++) {
				ImageView imageTile = (ImageView) tableLayoutRow.getChildAt(col);
				gameBoard[row][col] = imageTile;
			}
		}
		
		textViewLevel = (TextView) v.findViewById(R.id.textViewLevel);
		//Set Touch Event Listener
		tableLayoutGameBoard.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//Get position of selected tile from XY coordinate
				Point p = getTileFromPoint((int)event.getX(), (int)event.getY());
				if(p != null) {
					touchEvent(p.x, p.y, event);
				}
				return true;
			}
			
		});

		return v;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		//Update game board view from model
		controller.updateView();
		//Update game control fragment
		controller.updateControl();
	}
	//Convert Touch event XY coordinate into tile coordinate on game board
	private Point getTileFromPoint(int x, int y) {
		for(int row = 0; row < rows; row++) {
			for(int col = 0; col < cols; col++) {
				//Get tile image 
				ImageView imageTile = gameBoard[row][col];
				//Get parent view (table row)
				View parentView = (View) imageTile.getParent();
				//Get parent rect coordinates wrt board view
				Rect parentRect = new Rect();
				parentView.getHitRect(parentRect);
				//Get image rect coordinates wrt table row (ie parent)
				Rect imageRect = new Rect();
				imageTile.getHitRect (imageRect);
				//Add table row and tile coordinates
				imageRect.offset(parentRect.left, parentRect.top);
				//Check if event coordinates within tile rect coordinates
				if(imageRect.contains(x, y)){
					return new Point(row, col);
				}
			}
		}
		return null;
	}
	
	//Process touch event
	public void touchEvent(int row, int col, MotionEvent event) {
		switch(event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//Save action down event coordinates first
			downRow = row;
			downCol = col;
			break;
			
		case MotionEvent.ACTION_UP:
			//If down coordinate defined, generate die move event
			//Otherwise, die click event
			if(downRow >= 0 && downCol >= 0) {
				if(downRow == row && downCol == col) {
					controller.diceClick(row, col);					
				} else {
					controller.diceMove(downRow, downCol, row, col);					
				}
				downRow = -1;
				downCol = -1;
			}			
			break;	
		}	
	}
	
	public void setLevelNumber(int levelNumber) {
		String strLevel = getResources().getString(R.string.level);
		strLevel = strLevel + levelNumber;
		textViewLevel.setText(strLevel);
	}
	
	public void setTile(int row, int col, TILE_TYPES tile) {
		ImageView imageTile = gameBoard[row][col];
		imageTile.setImageResource(tile.getResId());
	}	
}
